package com.CST.controller;


import com.CST.annotation.ResultObjectAnnotation;
import com.CST.common.api.Response;
import com.CST.common.api.ResultObject;
import com.CST.common.constant.DmlTypeConstant;
import com.CST.entity.RuleAmortization;
import com.CST.entity.RuleCompelamortization;
import com.CST.entity.RuleDirectamortization;
import com.CST.entity.selectInfo;
import com.CST.service.IRuleCompelamortizationService;
import com.CST.util.PageHelperTool;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ly
 * @since 2021-10-27
 */
@Api(tags = {"强制分摊"})
@RestController
@RequestMapping("/ruleCompelamortization")

public class RuleCompelamortizationController {

    @Autowired
    private IRuleCompelamortizationService targetService;


    @PostMapping("/addRuleCompelamortization")
    @ApiOperation("新增,添加科室部门对应数据,必传参数:ruleCode:规则edExtraFirstCode = 1的edCode,yard :1")
    @ResultObjectAnnotation(value = DmlTypeConstant.INSERT, successMessage = "新建或编辑成功!", errorMessage = "新建或编辑失败!")
    public ResultObject addRuleCompelamortization(@RequestBody RuleCompelamortization entity) {
        Response result = targetService.addRuleCompelAmortization(entity);
        if (entity.getDeptname() == null || entity.getTodeptname() == null) {
            return ResultObject.errorReturn("添加的科室名称或者部门不能为空!");
        }
        return ResultObject.resultReturn(result);
    }

    @PostMapping("/insertBatchRuleCompelAmortization")
    @ApiOperation("继承添加")
    public ResultObject insertBatchRuleCompelAmortization(@RequestBody RuleCompelamortization entity) {

        Response result = targetService.insertBatchRuleCompelAmortization(entity);
        return ResultObject.resultReturn(result);
    }

    @PostMapping("/getCompelAmortization")
    @ApiOperation("获取强制分摊(查询)")
    public ResultObject getCompelAmortization(@RequestBody RuleCompelamortization entity) {
        List<RuleCompelamortization> result = targetService.getRuleCompelAmortization(entity);
        if (CollectionUtils.isEmpty(result)) {
            return ResultObject.successReturn(new ArrayList<RuleCompelamortization>());
        }
        return ResultObject.successReturn(result);
    }

    @PostMapping("/getRuleCompelAmortizationById")
    @ApiOperation("获取科室分摊ByID")
    public ResultObject getRuleCompelAmortizationById(@RequestBody RuleCompelamortization entity) {
        RuleCompelamortization result = targetService.getRuleCompelAmortizationById(entity);
        return ResultObject.successReturn(result);
    }

    @PostMapping("/DeleteCompelAmortization")
    @ApiOperation("删除强制分摊")
    public ResultObject DeleteCompelAmortization(@RequestBody List<Integer> ids) {
        if (ids == null || ids.stream().count() == 0) {
            return ResultObject.errorReturn("请选择要删除的记录");
        }
        Response result = targetService.batchDeleteRuleCompelAmortization(ids);
        return ResultObject.resultReturn(result);
    }

    @PostMapping("/getYearMonth")
    @ApiOperation("获取年月下拉数据")
    public ResultObject getYearMonth() {
        List<selectInfo> result = targetService.getYearMonth();
        if (CollectionUtils.isEmpty(result)) {
            return ResultObject.successReturn(new ArrayList<>());
        }
        return ResultObject.successReturn(result);
    }

    @PostMapping("/updateCompelamortization")
    @ApiOperation("修改科室部门对应数据,必传参数:ruleCode:规则edExtraFirstCode = 1的edCode,yard :1")
    public ResultObject updateCompelamortization(@RequestBody RuleCompelamortization entity) {
        if (entity.getId() == null || entity.getId() == 0) {
            return ResultObject.errorReturn("未获取到记录ID!");
        }
        Response result = targetService.modifyRuleCompelAmortization(entity);
        return ResultObject.resultReturn(result);
    }




    @GetMapping("/getById/{id}")
    @ApiOperation("查看")
    @ResultObjectAnnotation(DmlTypeConstant.SELECT)
    public Object getById(@PathVariable String id) {
        return targetService.getById(id);
    }

}

